package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.Constants;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询所有狐狸等级
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> listAll() {
        List<NursingLevel> list = nursingLevelMapper.listAll();
        ArrayList<NursingLevelVo> listVo = new ArrayList<>();
        list.stream().forEach(nursingLevel -> {
            NursingLevelVo nursingLevelVo = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
            listVo.add(nursingLevelVo);
        });
        return listVo;
    }

    /**
     * 分页查询护理等级
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> search(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page =nursingLevelMapper.search(name, status);
        return PageResponse.of(page, NursingLevelVo.class);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void insert(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 根据id查询护理等级
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getNursingLevelById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getNursingLevelById(id);
        return BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 删除护理等级
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        //状态为启用的护理等级不能删除
        NursingLevel nursingLevelById = nursingLevelMapper.getNursingLevelById(id);
        if (SuperConstant.STATE_1.equals(nursingLevelById.getStatus()) ) {
            throw new BaseException(BasicEnum.DELETE_FAIL_FOR_STATUS);
        }
        nursingLevelMapper.deleteNursingLevelById(id);
    }

    /**
     * 修改护理等级状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }


}
